package com.qf.java2107.yb.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MessageConstant;
import com.qf.java2107.yb.hotel.dao.FoodTypeDao;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.FoodType;
import com.qf.java2107.yb.hotel.service.FoodTypeService;
import com.qf.java2107.yb.hotel.utils.JdbcUtils;

import java.sql.SQLException;

/**
 * 菜系业务层实现
 * @author yb
 * @version 1.0
 * @date 2021-10-25
 **/
public class FoodTypeServiceImpl implements FoodTypeService {

    private FoodTypeDao foodTypeDao = (FoodTypeDao) BeanFactory.getBean("foodTypeDao");


    @Override
    public ResultVO findAll() {
        try {
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS, foodTypeDao.findAll());
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.FOODTYPE_QUERY_FAIL, e.getMessage());
        }
    }

    @Override
    public ResultVO findLikeTypeName(String typeName) {
        try {
            /**
             * 处理菜系名称
             */
            if(StringUtils.isEmpty(typeName)) {
                typeName = "%%";
            } else {
                typeName = "%" + typeName.trim() + "%";
            }
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS, foodTypeDao.findLikeTypeName(typeName));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.FOODTYPE_QUERY_FAIL, e.getMessage());
        }
    }

    @Override
    public ResultVO findByTypeName(String typeName) {

        try {
            if(StringUtils.isEmpty(typeName)) {
                return ResultVO.error(MessageConstant.FOODTYPENAME_IS_NOT_NULL);
            }

            Integer foodTypeId = foodTypeDao.findByTypeName(typeName);
            if(null == foodTypeId) {
                return ResultVO.ok("菜系名称不存在，可以使用！");
            } else {
                return ResultVO.error("菜系名称已存在，请更换！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }

    }

    @Override
    public ResultVO save(FoodType foodType) {
        try {
            JdbcUtils.begin();
            foodTypeDao.save(foodType);
            JdbcUtils.commit();
            return ResultVO.ok("菜系增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollback();
            return ResultVO.error("菜系增加失败", e.getMessage());
        }
    }

    @Override
    public ResultVO deleteById(int id) {
        try {
            JdbcUtils.begin();
            foodTypeDao.deleteById(id);
            JdbcUtils.commit();
            return ResultVO.ok("菜系删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollback();
            return ResultVO.error("菜系删除失败", e.getMessage());
        }
    }

    @Override
    public ResultVO findById(int id) {
        //FoodType foodType = foodTypeDao.findById(id);
        try {
            return ResultVO.ok(MessageConstant.FOODTYPE_QUERY_SUCCESS, foodTypeDao.findById(id));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.FOODTYPE_QUERY_FAIL, e.getMessage());
        }
    }

    @Override
    public ResultVO update(FoodType foodType) {
        try {
            JdbcUtils.begin();
            foodTypeDao.update(foodType);
            JdbcUtils.commit();
            return ResultVO.ok("菜系更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollback();
            return ResultVO.error("菜系更新失败", e.getMessage());
        }
    }
}
